// pages/myaddress/myaddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,//修改地址弹出层    初始值为false
    nowdata:{},
    addressid:null
  },
  // 弹出层弹出
  showPopup() {
    this.setData({ show: true });
  },
  // 弹出层隐藏
  onClose() {
    this.setData({ show: false });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      nowdata:JSON.parse(options.item),
      expressNumber:JSON.parse(options.item).expressNumber,
      from:options.from,
      addcanchange:options.addcanchange
    })
    this.setData({
      addressid:this.data.nowdata.addressid
    });
    if(this.data.from == 'score'){
      this.getDefaultAddress()
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // if(!this.data.addressid && this.data.addressid != '' ){
    //   this.getaddressData()
    // }else{
    //   this.getDefaultAddress()
    // }
    if(this.data.addressid){
      this.getaddressData()
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
    // 查询具体地址信息
    getaddressData(){
      var that = this
      wx.request({
        url: getApp().globalData.baseUrl + '/wago/address/selectByID',
        data: {
          addressid:that.data.addressid
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res){
          // success
          that.setData({
            address:res.data.data,
            nowdata2:res.data.data,
            nname:res.data.data.name,
            naddress:res.data.data.address,
            nphone:res.data.data.phone

          })
        },
        fail: function() {
          // fail
        },
        complete: function() {
          // complete
        }
      })
    },
  tochangeEdit(e){
    console.log('e.currentTarget.dataset.addressid',e.currentTarget.dataset.addressid)
    var vm = this;
    wx.navigateTo({
      url: '/pages/orderEditAddress3/orderEditAddress3?orderid=' + e.currentTarget.dataset.orderid
      +'&addressid=' + e.currentTarget.dataset.addressid +'&from='+vm.data.from
    })
  },
  //获取默认地址
  getDefaultAddress:function(){
    var vm = this;
        var data = {
          userid:getApp().globalData.openid,
        }
      wx.request({
          url: getApp().globalData.baseUrl + '/wago/address/selectDefaultAddress',
          data,
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
              vm.setData({
                address:res.data.data,
                addressid:res.data.data.addressid,
                nowdata2:res.data.data,
                nname:res.data.data.name,
                naddress:res.data.data.address,
                nphone:res.data.data.phone
              })
              console.log(vm.data.addressid)
              
          }
        })
  },
  changeData:function(e){
    var type = e.currentTarget.dataset.type;
    var value = e.detail.value;
    this.setData({
      [type]:value
    })
  },
  //下单
  confirmOrder:function(){

    var that = this;
     var data = {fulWagoOrder: {
      userid:getApp().globalData.openid,
      status:1,
      cannel:1,
      productNum:1,
      productJifen:that.data.nowdata.jifen,
      addressid:that.data.addressid,
      remarks:that.data.nremark,
      productid:that.data.nowdata.productid,
    }};
    if(this.data.addressid){
    wx.request({
        url: getApp().globalData.baseUrl + '/wago/order/add',
        data,
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res){
            if(res.data.flag){


                          //改变积分
            var changejifen = -that.data.nowdata.jifen;
            var changename = '兑换商品';
            var userid = getApp().globalData.openid;
            wx.request({
              url: getApp().globalData.baseUrl + '/wago/jifen/changeJifen',
              method: 'post',
              data: {
                userid,
                changename,
                changejifen
              },
              success: function (res) {


              }
            })

              wx.showToast({
                title: '兑换成功',
                duration: 2000,
                icon: 'none'
              });
              wx.requestSubscribeMessage({
                tmplIds: ['ru3cMSQKFurYaBL_Czpnv9xVzMFWUhVeqMwxDMWabXE'],
                success (res) { 
                  
                }
              })
                wx.navigateTo({
                  url: "/pages/myexchange/myexchange"
                })
            }else{
              var me = res.data.message;
              wx.showToast({
                title: me,
                duration: 2000,
                icon: 'none'
              });
            }
           
        }
      })
    }else{
      wx.showToast({
        title: '请先添加地址',
        duration: 2000,
        icon: 'none'
      });
    }





  },
  clearinfo:function(){
    this.setData({
      nowdata2:''
    })
  },
  logistics(){
    wx.navigateTo({
      url: '/pages/logistics/logistics?expressNumber=' + this.data.expressNumber,
    })
  },
  defaultinfo:function(){
    this.setData({
      nowdata2:this.data.address
    })
  },
  submitconfirm:function(){
    var that = this;
    console.log(this.data.address)
    if(this.data.address){
          var data = {
      addressid:that.data.address.addressid,
      userid:getApp().globalData.openid,
      name:that.data.nname,
      phone:that.data.nphone,
      address:that.data.naddress,
      status:that.data.address.status,
      prefer:that.data.address.prefer
    }
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/address/update',
      data,
      method: 'POST', 
      success: function(res){
          if(res.data.message == '修改成功'){
            that.setData({
               show: false,
               address:data
               });
            wx.showToast({
              title: '修改成功',
              duration: 2000,
              icon: 'none'
            });
          }else{
            wx.showToast({
              title: '修改失败',
              duration: 2000,
              icon: 'none'
            });
          }
      }
    })
    }else{
      
    }

  }
})